package com.edu.exam.entity.examination.paper.technical;

import jakarta.persistence.*;
import lombok.Data;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.experimental.Accessors;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;
import java.util.List;
import com.edu.exam.enums.examination.paper.SegmentType;
import com.edu.exam.enums.examination.paper.SegmentationProcessingStatus;
import com.edu.exam.enums.examination.paper.SegmentationProcessingAlgorithm;
import com.edu.exam.enums.examination.paper.SegmentationManualReviewStatus;
import com.edu.exam.entity.examination.paper.technical.SegmentationConfig;

/**
 * 智能切分结果实体类
 *
 * 根据E-04-04 智能切分子模块设计实现
 * 对应exam_segmentation_result数据表
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class SegmentationResult {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 关联的切分任务ID，关联exam_segmentation_task表
     */
    @Column(name = "task_id", nullable = false)
    private Long taskId;

    /**
     * 关联的切分配置ID，关联exam_segmentation_config表
     */
    @Column(name = "config_id")
    private Long configId;

    /**
     * 切分片段类型：QUESTION_AREA（题目区域）、ANSWER_AREA（答题区域）、HEADER_AREA（表头区域）、FOOTER_AREA（表尾区域）、MARGIN_AREA（边距区域）、OTHER（其他）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "segment_type", nullable = false, length = 20)
    private SegmentType segmentType;

    /**
     * 题目编号（如果切分的是题目区域）
     */
    @Column(name = "question_number", length = 20)
    private String questionNumber;

    /**
     * 题目ID，关联question_bank表
     */
    @Column(name = "question_id")
    private Long questionId;

    /**
     * 区域序号（同一类型区域的编号）
     */
    @Column(name = "region_sequence")
    private Integer regionSequence;

    /**
     * 原始图像中的X坐标起点
     */
    @Column(name = "original_x_start")
    private Integer originalXStart;

    /**
     * 原始图像中的Y坐标起点
     */
    @Column(name = "original_y_start")
    private Integer originalYStart;

    /**
     * 原始图像中的宽度
     */
    @Column(name = "original_width")
    private Integer originalWidth;

    /**
     * 原始图像中的高度
     */
    @Column(name = "original_height")
    private Integer originalHeight;

    /**
     * 切分后的X坐标起点（可能经过坐标校正）
     */
    @Column(name = "segmented_x_start")
    private Integer segmentedXStart;

    /**
     * 切分后的Y坐标起点（可能经过坐标校正）
     */
    @Column(name = "segmented_y_start")
    private Integer segmentedYStart;

    /**
     * 切分后的宽度
     */
    @Column(name = "segmented_width")
    private Integer segmentedWidth;

    /**
     * 切分后的高度
     */
    @Column(name = "segmented_height")
    private Integer segmentedHeight;

    /**
     * 处理状态：SUCCESS（成功）、FAILED（失败）、PARTIAL（部分成功）、REJECTED（被拒绝）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "processing_status", nullable = false, length = 20)
    private SegmentationProcessingStatus processingStatus;

    /**
     * 切分置信度（0-100）
     */
    @Column(name = "segmentation_confidence")
    private Double segmentationConfidence;

    /**
     * 质量分数（0-100）
     */
    @Column(name = "quality_score")
    private Double qualityScore;

    /**
     * 边界清晰度分数（0-100）
     */
    @Column(name = "boundary_clarity_score")
    private Double boundaryClarityScore;

    /**
     * 内容完整性分数（0-100）
     */
    @Column(name = "content_completeness_score")
    private Double contentCompletenessScore;

    /**
     * 格式一致性分数（0-100）
     */
    @Column(name = "format_consistency_score")
    private Double formatConsistencyScore;

    /**
     * 检测到的问题数量
     */
    @Column(name = "detected_issues_count")
    private Integer detectedIssuesCount;

    /**
     * 严重问题数量
     */
    @Column(name = "critical_issues_count")
    private Integer criticalIssuesCount;

    /**
     * 主要问题数量
     */
    @Column(name = "major_issues_count")
    private Integer majorIssuesCount;

    /**
     * 次要问题数量
     */
    @Column(name = "minor_issues_count")
    private Integer minorIssuesCount;

    /**
     * 处理耗时（毫秒）
     */
    @Column(name = "processing_duration_ms")
    private Long processingDurationMs;

    /**
     * 使用的算法类型：TEMPLATE_MATCHING（模板匹配）、BOUNDARY_DETECTION（边界检测）、CONTOUR_BASED（基于轮廓）、HYBRID（混合）、DEEP_LEARNING（深度学习）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "algorithm_used", length = 30)
    private SegmentationProcessingAlgorithm algorithmUsed;

    /**
     * 算法参数（JSON格式）
     */
    @Column(name = "algorithm_parameters", length = 1000, columnDefinition = "TEXT")
    private String algorithmParameters;

    /**
     * 检测到的边界点（JSON格式）
     */
    @Column(name = "detected_boundary_points", length = 2000, columnDefinition = "TEXT")
    private String detectedBoundaryPoints;

    /**
     * 区域特征（JSON格式）
     */
    @Column(name = "region_features", length = 2000, columnDefinition = "TEXT")
    private String regionFeatures;

    /**
     * 切分问题描述（JSON格式）
     */
    @Column(name = "segmentation_issues", length = 2000, columnDefinition = "TEXT")
    private String segmentationIssues;

    /**
     * 是否需要人工审核
     */
    @Column(name = "requires_manual_review")
    private Boolean requiresManualReview;

    /**
     * 人工审核状态：PENDING（待审核）、APPROVED（已审核通过）、REJECTED（审核拒绝）、REVISED（已修订）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "manual_review_status", length = 20)
    private SegmentationManualReviewStatus manualReviewStatus;

    /**
     * 人工审核时间
     */
    @Column(name = "manual_review_time")
    private LocalDateTime manualReviewTime;

    /**
     * 人工审核人员ID，关联sys_user表
     */
    @Column(name = "manual_reviewer_id")
    private Long manualReviewerId;

    /**
     * 人工审核时调整的边界（JSON格式）
     */
    @Column(name = "manual_adjusted_boundary", length = 1000, columnDefinition = "TEXT")
    private String manualAdjustedBoundary;

    /**
     * 人工审核意见
     */
    @Column(name = "manual_review_comment", length = 1000, columnDefinition = "TEXT")
    private String manualReviewComment;

    /**
     * 是否有手动校正
     */
    @Column(name = "has_manual_correction")
    private Boolean hasManualCorrection;

    /**
     * 校正次数
     */
    @Column(name = "correction_count")
    private Integer correctionCount;

    /**
     * 最后校正时间
     */
    @Column(name = "last_correction_time")
    private LocalDateTime lastCorrectionTime;

    /**
     * 最后校正人员ID，关联sys_user表
     */
    @Column(name = "last_corrector_id")
    private Long lastCorrectorId;

    /**
     * 关联的切分图像ID，关联exam_segmented_image表
     */
    @Column(name = "segmented_image_id")
    private Long segmentedImageId;

    /**
     * 扩展属性（JSON格式）
     */
    @Column(name = "additional_attributes", length = 2000, columnDefinition = "TEXT")
    private String additionalAttributes;

    // ================================
    // JPA 关联关系
    // ================================

    /**
     * 关联的切分任务
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_id", insertable = false, updatable = false)
    private SegmentationTask segmentationTask;

    /**
     * 关联的切分配置
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "config_id", insertable = false, updatable = false)
    private SegmentationConfig segmentationConfig;

    /**
     * 关联的切分图像
     */
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "segmented_image_id", insertable = false, updatable = false)
    private SegmentedImage segmentedImage;

    // ================================
    // 审计字段
    // ================================

    @CreationTimestamp
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @UpdateTimestamp
    @Column(name = "updated_time")
    private LocalDateTime updatedTime;

    @Version
    @Column(name = "version")
    private Integer version = 0;

    @Column(name = "is_deleted")
    private Boolean isDeleted = false;
}